﻿using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class AcessoFeira : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnEntrar_Click(object sender, ImageClickEventArgs e)
    {
        if (ValidaUsuario(txtCPF.Text, txtSenha.Text))
            Response.Redirect("candidatocadastropre.aspx");
        else
            Response.Redirect("mensagens.aspx?id=1");
    }

    protected bool ValidaUsuario(string usuario, string senha)
    {
        bool valida = false;
        SqlConnection conexaoIntranet = new SqlConnection(ConfigurationManager.ConnectionStrings["conexao_intranet"].ConnectionString);
        SqlCommand consultaLogin = null;
        SqlDataReader drLogin;
        try
        {
            if (conexaoIntranet != null && conexaoIntranet.State != ConnectionState.Open)
                conexaoIntranet.Open();
            string instrucao = "Select CodUsuario, Usr, Senha, CadastraFuncao FROM CamaraLogin where USR = '" + usuario + "' and SENHA = '" + senha + "' and Excluido = 0";
            consultaLogin = new SqlCommand(instrucao, conexaoIntranet);
            drLogin = consultaLogin.ExecuteReader();
            if (drLogin.HasRows)
            {
                valida = true;
                drLogin.Read();
                Session["codUsuario"] = drLogin["CodUsuario"].ToString();
                Session["login"] = txtCPF.Text;
				Session["CadastraFuncao"] = drLogin["CadastraFuncao"].ToString();
            }
            drLogin.Close();
        }
        finally
        {
            conexaoIntranet.Close();
        }
        return valida;
    }
}